const async = require('async');
const services = require('../../services');

const commonService = services.common;
const eLancoService = services.eLanco;

module.exports = (req, res, callback) => {
  const { WfRunInstance: formDetailModel, WfRunFlowDetail: approvalDetailModel } = commonService.getModels();
  const formId = req.body.formId;
  const id = req.params.id;
  const getFormDetail = (next) => {
    eLancoService.getFormDetailIncludeAll(formId, id, next);
  };
  const deleteFormDetail = (data, next) => {
    formDetailModel.upsert({ id, deleted: true }, next);
  };
  const deleteApprovalList = (formDetail, next) => {
    approvalDetailModel.updateAll({ instanceId: id }, { deleted: true }, next);
  };
  async.waterfall([getFormDetail, deleteFormDetail, deleteApprovalList], callback);
};
